def length_of_longest_substring(str1, k):
  start = 0
  max_length = 0
  letters_num = {}

  for end in range(len(str1)):
    right_key = str1[end]
    if (right_key not in letters_num):
      letters_num[right_key] = 0
    letters_num[right_key] += 1
    letters_num_list = sorted(letters_num.values(),reverse=True)

    while((end-start+1-letters_num_list[0])>k):
      left_key = str1[start]
      letters_num[left_key] -= 1
      start += 1
      if(letters_num[left_key]==0):
        del letters_num[left_key]

      letters_num_list = sorted(letters_num.values(),reverse=True)
 
    max_length = max(max_length,end-start+1)
  # TODO: Write your code here
  return max_length

def main():
  print(length_of_longest_substring("aabccbb", 2))
  print(length_of_longest_substring("abbcb", 1))
  print(length_of_longest_substring("abccde", 1))


main()

